﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式.服务定位器模式
{
   public class Cache
    {
        private List<IService> services;
        public Cache()
        {
            services = new List<IService>();
        }

        public IService GetService(string serviceName)
        {
            foreach(IService service in services)
            {
                if (service.GetName().Equals(serviceName))
                {
                    Console.WriteLine("Returning cached " + services + " object");
                    return service;
                }
            }
            return null;
        }

        public void AddService(IService newService)
        {
            bool exists = false;
            foreach(IService service in services)
            {
                if (service.GetName().Equals(newService.GetName()))
                {
                    exists = true;
                }
            }
            if (!exists)
            {
                services.Add(newService);
            }
        }
    }
}
